#!/usr/local/bin/php
<?php

/**
 *      [CodeJm!] Author CodeJm[codejm@163.com].
 *
 *
 *      $Id: Cli.php 2015-08-28 15:13:50 codejm $
 */

// 常用目录定义
//header("Content-type: text/html; charset=utf-8");
define('DS', '/');
define('PUBLIC_PATH', dirname(__FILE__).DS);
define('BASE_PATH', realpath(dirname(__FILE__).DS.'..').DS);
define('APP_PATH', realpath(dirname(__FILE__).DS.'..'.DS.'application').DS);
// composer
require_once BASE_PATH.'vendor/autoload.php';

// 框架入口
$app  = new Yaf_Application(APP_PATH.'conf/application.ini');
$app->bootstrap();
$config = Yaf_Application::app()->getConfig()->toArray();

// 参数格式化
$params = array();
parse_str(implode('&', array_slice($argv, 1)), $params);
// 退出
$params['pid_path']  = !empty($params['pid_path']) ? $params['pid_path'] : $config['resque']['pid_path'];
if(isset($params['stop'])){
    $quitstyle = SIGQUIT;
    if($params['stop'] == 'now')
        $quitstyle = SIGKILL;
    $pidfiles = glob($params['pid_path']."*");
    if($pidfiles){
        foreach ($pidfiles as $pidfile) {
            $pid = file_get_contents($pidfile);
            posix_kill($pid, $quitstyle);
            unlink($pidfile);
        }
    }
    unset($pidfiles);
    exit('success');
}
// 参数处理
$params['queue']     = !empty($params['queue']) ? $params['queue'] : $config['resque']['queue'];
$params['host']      = !empty($params['host']) ? $params['host'] : $config['resque']['redis']['host'];
$params['prefix']    = !empty($params['prefix']) ? $params['prefix'] : $config['resque']['redis']['prefix'];
$params['interval']  = !empty($params['interval']) ? (int)$params['interval'] : $config['resque']['interval'];
$params['count']     = !empty($params['count']) ? (int)$params['count'] : $config['resque']['count'];

// redis config and redis prefix
Resque::setBackend($params['host'], 1);
if(!empty($params['prefix'])) {
    Resque_Redis::prefix($params['prefix']);
}

// 加载类文件
$jobfiles = glob(APP_PATH.'job/*.php');
foreach($jobfiles as $job) {
    include $job;
}
unset($jobfiles);

if($params['count'] > 1) {
    for($i=0; $i <$params['count']; ++$i) {
		$pid = pcntl_fork();
        if($pid == -1) {
            die('Could not fork worker'.$i."\n");
        } else if(!$pid) {
            // Child, start the worker
            $queues = explode(',', $params['queue']);
            $worker = new Resque_Worker($queues);
            if($params['pid_path']) {
                $filename = $params['pid_path'].$params['queue'].'.'.$i.'.'.getmypid();
                file_put_contents($filename, getmypid()) or
                    die('Could not write PID information to ' . $filename);
            }
            echo 'Starting worker '.$worker."\n";
            $worker->work($params['interval']);
            break;
        }
    }
} else {
    // Start a single worker
    $queues = explode(',', $params['queue']);
    $worker = new Resque_Worker($queues);

    if($params['pid_path']) {
        $filename = $params['pid_path'].$params['queue'].'.'.getmypid();
        file_put_contents($filename, getmypid()) or
            die('Could not write PID information to ' . $filename);
    }
    echo 'Starting worker '.$worker;
    $worker->work($params['interval']);
}

?>
